package com.tw.marsrover;

public class Position{
    private int xCoordinate;
    private int yCoordinate;
    private Direction direction;

    public Position(int xCoordinate, int yCoordinate, String direction) {
        this.xCoordinate = xCoordinate;
        this.yCoordinate = yCoordinate;
        this.direction = Direction.valueOf(direction);

    }

    public Position(Position position) {
        this.xCoordinate = position.xCoordinate;
        this.yCoordinate = position.yCoordinate;
        this.direction = position.direction;

    }


    public String getDirection() {
        return direction.getDirectionName();
    }

    public void turnLeft() {
        direction = direction.changeToLeft();
    }

    public void turnRight() {
        direction = direction.changeToRight();
    }



    @Override
    public boolean equals(Object other){
        if(other == this)
            return true;

        if(!(other instanceof Position))
            return false;

        Position positionOther = (Position) other;

        return this.xCoordinate == positionOther.xCoordinate &&
               this.yCoordinate == positionOther.yCoordinate &&
                this.direction.getDirectionName().equals(positionOther.direction.getDirectionName());
    }

    public Position moveAhead() {
        Position newPosition = new Position(this);

        if(direction == Direction.North)
            newPosition.yCoordinate++;
        if(direction == Direction.East)
            newPosition.xCoordinate++;
        if(direction == Direction.South)
            newPosition.yCoordinate--;
        if(direction == Direction.West)
            newPosition.xCoordinate--;

        return newPosition;
    }

    public int getXCoordinate() {
        return this.xCoordinate;
    }

    public int getYCoordinate() {
        return this.yCoordinate;
    }
}



